package org.springblade.modules.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.restaurant.entity.Area;
import org.springblade.modules.restaurant.entity.CustomerSituation;
import org.springblade.modules.restaurant.service.IAreaService;
import org.springblade.modules.restaurant.service.ICustomerSituationService;
import org.springblade.modules.restaurant.vo.CustomerSituationVO;
import org.springblade.modules.restaurant.wrapper.CustomerSituationWrapper;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/restaurant/customer")
@Tag(name = "API - 客情信息")
public class ApiCustomerSituationController {

	private ICustomerSituationService customerSituationService;
	private IAreaService areaService;
	private IUserService userService;

	/**
	 * 分页 客情信息
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入customerSituation")
	public R<IPage<CustomerSituation>> list(@ParameterObject CustomerSituation customerSituation, @ParameterObject Query query) {
		IPage<CustomerSituation> pages = customerSituationService.page(Condition.getPage(query),
			Condition.getQueryWrapper(customerSituation).orderByDesc("create_time"));
		return R.data(pages);
	}


	/**
	 * 自定义分页 客情信息
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入customerSituation")
	public R<IPage<CustomerSituationVO>> page(@ParameterObject CustomerSituation customerSituation, @ParameterObject Query query) {

		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);

		User userInfo = userService.getById(userId);
		LambdaQueryWrapper<CustomerSituation> wrapper = new LambdaQueryWrapper<>();
		// 非订台 或 管理员 仅允许查看自己数据
		if (userInfo.getIsBooker() != 1 && !role.contains("admin")) {
			wrapper.eq(CustomerSituation::getCreateUser, userId);
		}
		if (StrUtil.isNotBlank(customerSituation.getContact())) {
			wrapper.like(CustomerSituation::getGuestName, customerSituation.getContact()).or().like(CustomerSituation::getContact, customerSituation.getContact());
		}
		wrapper.orderByDesc(BaseEntity::getCreateTime);
		IPage<CustomerSituation> pages = customerSituationService.page(Condition.getPage(query), wrapper);

		return R.data(CustomerSituationWrapper.build().pageVO(pages));
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入customerSituation")
	public R<CustomerSituation> detail(CustomerSituation customerSituation) {
		CustomerSituation detail = customerSituationService.getOne(Condition.getQueryWrapper(customerSituation));
		return R.data(detail);
	}

	@ApiLog(value = "新增或修改客情信息")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入customerSituation")
	public R submit(@Valid @RequestBody CustomerSituation customerSituation) {
		// 查询归属区域
		Area area = areaService.getParentArea(customerSituation.getRoomId());
		if (area != null) {
			customerSituation.setAreaName(area.getAreaName());
			customerSituation.setAreaId(area.getId());
		}
		return R.status(customerSituationService.saveOrUpdate(customerSituation));
	}

}
